package com.leyou.cart.controller;
import com.leyou.cart.entity.Favorite;
import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 这是测试当前处理器是否能获取到当前请求的请求头信息
     * @param request
     * @return
     */
    @GetMapping("/testHeader")
    public ResponseEntity<Void> testHeader(HttpServletRequest request){
        System.out.println(request);
        String userId = request.getHeader("xxxx");
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 添加购物车
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){
        cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Cart>> queryCarts(){
        List<Cart> carts = cartService.queryCarts();
        return ResponseEntity.ok(carts);
    }

    /**
     * 合并购物车
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<Void> addCarts(@RequestBody List<Cart> carts){
        cartService.addCarts(carts);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除购物车中的商品并在购物车下方回显
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCartBySkuId(@PathVariable("id")Long id){
        cartService.deleteCartBySkuId(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }
    /**
     * 删除购物车
     * @return
     */
    @GetMapping("/delete")
    public void deleteCart(){
        boolean flag = cartService.deleteCart();
        System.out.println("购物车是否删除成功:"+flag);
    }
    /**
     * 添加收藏
     * @return
     */

    @PostMapping("/addfavorite")
    public ResponseEntity<Void> addFavorite(@RequestParam("skuid") String skuid){
        cartService.addFavorite(skuid);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 查询用户所有收藏
     * @return
     */
    @GetMapping("/favorite/list")
    public ResponseEntity<List<Favorite>> queryFavorites(){
        List<Favorite> favorites=cartService.queryFavorites();
        return ResponseEntity.ok(favorites);
    }
    /**
     * 删除用户所有收藏
     * @return
     */
    @DeleteMapping("/user/favorite/{skuId}")
    public ResponseEntity<Favorite> deleteAndShowFavoriteBySkuId(@PathVariable("skuId")Long skuId){

        Favorite favorite=cartService.deleteAndShowFavoriteBySkuId(skuId);
        return ResponseEntity.ok(favorite);
    }
    /**
     * 用户收藏中加入购物车
     */
    @DeleteMapping("/favorite/addCart/{skuId}")
    public ResponseEntity<Void> addCartFromFavorite(@PathVariable("skuId") Long skuId){
        cartService.addCartFromFavorite(skuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
